<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_product\Entity\ShusyProductComment;
use EntityListWrapper;

/**
 * @OA\Schema(
 *   schema="CommentItem",
 *   type="object",
 *   description="CommentItem"
 * )
 */
class CommentItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_product_comment';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyProductComment) {
      $imgs = [];
      $imgs_s = [];
      $field_images = $this->wrapper->field_images;
      if ($field_images instanceof EntityListWrapper) {
        foreach ($field_images->getIterator() as $item) {
          $imgs[] = $this->imageStyleUrl('CommentItemImgs', $item->value());
          $imgs_s[] = $this->imageStyleUrl('CommentItemImgsOriginal', $item->value());
        }
      }
      $value = [
        //ID
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity_id,
        /**
         * @OA\Property(
         *   property="user",
         *   ref="#/components/schemas/User",
         *   description="评论用户",
         * ),
         */
        'user' => User::get($this->entity->uid),
        //时间 Y-m-d
        /**
         * @OA\Property(
         *   property="created",
         *   type="string",
         *   description="时间",
         * ),
         */
        'created' => (string) $this->formatDate($this->entity->created, 'Y-m-d'),
        //评分 1 - 5
        /**
         * @OA\Property(
         *   property="star",
         *   type="integer",
         *   description="评分 1 - 5",
         * ),
         */
        'star' => (int) $this->entity->score,
        //评论内容
        /**
         * @OA\Property(
         *   property="info",
         *   type="string",
         *   description="评论内容",
         * ),
         */
        'info' => (string) $this->entity->content,
        //评论图片
        /**
         * @OA\Property(
         *   property="imgs",
         *   type="array",
         *   description="评论缩略图",
         *   @OA\Items(type="string")
         * ),
         */
        'imgs' => $imgs,
        //评论原图
        /**
         * @OA\Property(
         *   property="imgs_original",
         *   type="array",
         *   description="评论原图",
         *   @OA\Items(type="string")
         * ),
         */
        'imgs_original' => $imgs_s,
      ];
      $this->merge($value);
    }
  }
}
